package com.anyou.mall.goo.specs.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.utils.DateUtils;
import com.anyou.mall.goo.goods.mapper.GooGoodsMapper;
import com.anyou.mall.goo.specs.Param.addSpecsParam;
import com.anyou.mall.goo.specs.domain.SpeSpecsValue;
import com.anyou.mall.goo.specs.mapper.SpeSpecsValueMapper;
import com.anyou.mall.sho.domain.CarPartsShop;
import com.anyou.mall.sho.domain.CarPartsShopAudit;
import com.anyou.mall.sho.mapper.CarPartsShopAuditMapper;
import com.anyou.mall.sho.mapper.CarPartsShopMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.anyou.mall.goo.specs.mapper.SpeSpecsMapper;
import com.anyou.mall.goo.specs.domain.SpeSpecs;
import com.anyou.mall.goo.specs.service.ISpeSpecsService;

/**
 * 规格管理Service业务层处理
 *
 * @author ruoyi
 * @date 2022-03-25
 */
@Service
public class SpeSpecsServiceImpl extends ServiceImpl<SpeSpecsMapper, SpeSpecs> implements ISpeSpecsService
{
    @Autowired
    private SpeSpecsMapper speSpecsMapper;

    @Autowired
    private SpeSpecsValueMapper speSpecsValueMapper;

    @Autowired
    private GooGoodsMapper gooGoodsMapper;

    //汽配商mapper
    @Autowired
    private CarPartsShopMapper carPartsShopMapper;

    //汽配商审核
    @Autowired
    private CarPartsShopAuditMapper carPartsShopAuditMapper;

    /**
     * 查询规格管理
     *
     * @param specsId 规格管理主键
     * @return 规格管理
     */
    @Override
    public SpeSpecs selectSpeSpecsBySpecsId(Long specsId)
    {
        SpeSpecs speSpecs = speSpecsMapper.selectById(specsId);
        List<SpeSpecsValue> speSpecsValues = speSpecsValueMapper.selectList(new LambdaQueryWrapper<SpeSpecsValue>()
                .eq(SpeSpecsValue::getSpecsId, specsId));
        speSpecs.setSpecsValueList(speSpecsValues);
        List<String> contentIds = speSpecsValues.stream().map(model->{
            return model.getSpecsValue();
        }).collect(Collectors.toList());
        String specsValue = StringUtils.join(Arrays.asList(contentIds), ";");
        speSpecs.setSpecsValue(specsValue);
        /*CarPartsShop carPartsShop = carPartsShopMapper.selectById(speSpecs.getCarPartsShopId());
        CarPartsShopAudit carPartsShopAudit = carPartsShopAuditMapper.selectById(carPartsShop.getCarPartsShopAuditId());
        speSpecs.setShopName(carPartsShopAudit.getShopName());*/

        return speSpecs;
    }

    /**
     * 查询规格管理列表
     *
     * @param speSpecs 规格管理
     * @return 规格管理
     */
    @Override
    public List<SpeSpecs> selectSpeSpecsList(SpeSpecs speSpecs)
    {
        List<SpeSpecs> speSpecs1 = speSpecsMapper.selectSpeSpecsList(speSpecs);
        speSpecs1.forEach(e->{
            //查询规格列表
            e.setSpecsValueList(speSpecsValueMapper
             .selectList(new LambdaQueryWrapper<SpeSpecsValue>()
                     .eq(SpeSpecsValue::getDelFlag,0)
                     .eq(SpeSpecsValue::getSpecsId, e.getSpecsId())));
            List<SpeSpecsValue> speSpecsValues = speSpecsValueMapper
                    .selectList(new LambdaQueryWrapper<SpeSpecsValue>().eq(SpeSpecsValue::getSpecsId, e.getSpecsId()));
            List<String> contentIds = speSpecsValues.stream().map(model->{
                return model.getSpecsValue();
            }).collect(Collectors.toList());
            String specsValue = StringUtils.join(Arrays.asList(contentIds), ";");
            e.setSpecsValue(specsValue);
            //根据商家id查询店铺名称
           /* String shopName = carPartsShopAuditMapper.selectShopName(e.getStoreId());
            e.setShopName(shopName);*/
            /*CarPartsShop carPartsShop = carPartsShopMapper.selectOne(new LambdaQueryWrapper<CarPartsShop>().eq(CarPartsShop::getStoreId, e.getStoreId()));
            CarPartsShopAudit carPartsShopAudit = carPartsShopAuditMapper.selectOne(new LambdaQueryWrapper<CarPartsShopAudit>()
                    .eq(CarPartsShopAudit::getCarPartsShopAuditId, carPartsShop.getCarPartsShopAuditId()));*/
        });

        return speSpecs1;
    }

    /**
     * 新增规格管理
     *
     * @param speSpecs 规格管理
     * @return 结果
     */
    @Override
    public int insertSpeSpecs(SpeSpecs speSpecs)
    {
        speSpecs.setCreateTime(DateUtils.getNowDate());
        return speSpecsMapper.insertSpeSpecs(speSpecs);
    }

    /**
     * 修改规格管理
     *
     * @param speSpecs 规格管理
     * @return 结果
     */
    @Override
    public int updateSpeSpecs(SpeSpecs speSpecs)
    {
        speSpecsMapper.updateById(speSpecs);
        speSpecs.getSpecsValueList().forEach(e->{
            speSpecsValueMapper.deleteSpeSpecsValueBySpecsValueId(speSpecs.getSpecsId());
        });
        speSpecs.getSpecsValueList().forEach(e->{
            e.setSpecsId(speSpecs.getSpecsId());
            e.setDelFlag("0");
            speSpecsValueMapper.insert(e);
        });
        return 1;
    }

    /**
     * 批量删除规格管理
     *
     * @param specsIds 需要删除的规格管理主键
     * @return 结果
     */
    @Override
    public int deleteSpeSpecsBySpecsIds(Long[] specsIds)
    {
        return speSpecsMapper.deleteSpeSpecsBySpecsIds(specsIds);
    }

    /**
     * 删除规格管理信息
     *
     * @param specsId 规格管理主键
     * @return 结果
     */
    @Override
    public int deleteSpeSpecsBySpecsId(Long specsId)
    {
        LambdaQueryWrapper<SpeSpecsValue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpeSpecsValue::getSpecsId,specsId);
        List<SpeSpecsValue> speSpecsValue = speSpecsValueMapper.selectList(queryWrapper);
        int i = speSpecsMapper.deleteById(specsId);
        speSpecsValue.forEach(e->{
            speSpecsValueMapper.deleteById(e.getSpecsValueId());
        });
        return i;
    }

    /**
     * 新增规格和规则值
     * @param addSpecsParam
     * @return
     */
    @Override
    public ApiResult insertSpecs(addSpecsParam addSpecsParam) {

        /*CarPartsShop carPartsShop = carPartsShopMapper.selectById(addSpecsParam.getCarPartsShopId());
        if (carPartsShop==null){
            return  ApiResult.success("汽配商不存在");
        }*/

        SpeSpecs speSpecs = new SpeSpecs();
        BeanUtils.copyProperties(addSpecsParam,speSpecs);
        /*speSpecs.setStoreId(carPartsShop.getStoreId());*/
        speSpecsMapper.insert(speSpecs);

        addSpecsParam.getSpecsValue().forEach(e->{
            SpeSpecsValue speSpecsValue = new SpeSpecsValue();
            speSpecsValue.setSpecsId(speSpecs.getSpecsId());
            speSpecsValue.setSpecsValue(e);

            speSpecsValueMapper.insert(speSpecsValue);
        });

        return ApiResult.success();
    }

    @Override
    public ApiResult<List<SpeSpecs>> getListByShopId(Long carPartsShopId) {
        CarPartsShop carPartsShop = carPartsShopMapper.selectById(carPartsShopId);
        if(carPartsShop == null){
            return ApiResult.error("汽配商编号错误或不存在");
        }
        List<SpeSpecs> list = speSpecsMapper.selectList(new LambdaQueryWrapper<SpeSpecs>().eq(SpeSpecs::getCarPartsShopId, carPartsShop.getCarPartsShopId()).eq(SpeSpecs::getDelFlag, "0"));
        if(!list.isEmpty()){
            list.forEach(e -> {
                List<SpeSpecsValue> specsValueList = speSpecsValueMapper.selectList(new LambdaQueryWrapper<SpeSpecsValue>().eq(SpeSpecsValue::getSpecsId, e.getSpecsId()).eq(SpeSpecsValue::getDelFlag, "0"));
                e.setSpecsValueList(specsValueList);
            });
        }
        return ApiResult.success(list);
    }


}
